<template>
  <div class="set-field-permission">
    <el-dialog
      :title="$t('权限条件设置')"
      v-if="dialogVisible"
      :visible.sync="dialogVisible"
      width="50%"
      :before-close="handleClose"
      :append-to-body="true"
    >
      <div class="set-field-permission-content">
        <el-button icon="el-icon-plus" type="text" v-if="ruleList.length == 0" @click="handleAdd">添加</el-button>
        <div
          class="set-field-permission-content-item"
          v-for="(item, index) in ruleList"
          :key="index"
        >
          <div class="set-field-permission-content-item-condition">
            <el-row class="list-row">
              <el-col :span="12">
                <el-row>
                  <el-col :span="4">{{ $t('如果：') }}</el-col>
                  <el-col :span="20">
                    <el-input
                      @focus="handleCondion(item, index)"
                      v-model="item.ruleTextShow"
                      readonly
                      type="textarea"
                      :rows="4"
                      :placeholder="$t('请选择')"
                    ></el-input>
                  </el-col>
                </el-row>
              </el-col>
              <el-col :span="11" style="margin-left: 15px">
                <el-col :span="4">{{ $t('则为：') }}</el-col>
                <el-col :span="20">
                  <div class="right-title">
                    <span
                      >{{ $t('【') }}{{ row.pageName }}{{ $t('】 ')
                      }}{{ row.parameterName }}
                    </span>
                    <span class="title-tips">{{ $t('设置以下权限') }}</span>
                  </div>
                  <el-row style="margin-top: 20px">
                    <el-col :span="24">
                      <el-radio-group
                        v-model="item.state"
                        @change="(arg) => changeState(arg, index)"
                      >
                        <el-radio :label="1">{{ $t('浏览') }}</el-radio>
                        <el-radio :label="2">{{ $t('编辑') }}</el-radio>
                        <el-radio :label="3">{{ $t('隐藏') }}</el-radio>
                        <el-radio :label="4">{{ $t('必填') }}</el-radio>
                      </el-radio-group>
                    </el-col>
                  </el-row>
                </el-col>
              </el-col>
            </el-row>
          </div>
          <div class="set-field-permission-content-item-opertaion">
            <img
              @click="handleAdd(index)"
              src="@/assets/images/standard/plus_icon.png"
              alt=""
            />
            <img
              @click="handleDel(index)"
              src="@/assets/images/standard/minus_icon.png"
              alt=""
            />
          </div>
        </div>
      </div>
      <span slot="footer" class="dialog-footer">
        <el-button @click="handleClose">{{ $t('取 消') }}</el-button>
        <el-button type="primary" @click="confirm">{{ $t('确 定') }}</el-button>
      </span>
    </el-dialog>
    <setCondition
      v-if="optionsVisible"
      v-model="optionsVisible"
      :id="id"
      :currentIndex="currentIndex"
      :row="row" 
      :rowData="rowData"
      :itemData="itemData"
      @setCondition="setCondition"
    />
  </div>
</template>
<script>
import setCondition from './setCondition.vue'
export default {
  components: {
    setCondition
  },
  props: {
    value: Boolean,
    row: {
      type: Object,
      default: () => {
        return {}
      }
    },
    id: {},
    rowData: {
      type: Object,
      default: () => {
        return {}
      }
    }
  },
  data() {
    return {
      dialogVisible: false,
      ruleList: [{ state: 2 }],
      currentIndex: null,
      optionsVisible: false,
      itemData: {},
      otherRuleList: [],
    }
  },
  created() {
    this.dialogVisible = this.value
    this.setData()
  },
  methods: {
    setData() {
      if (this.row.ruleList && this.row.ruleList.length) {
        // 存储其他的规则
        this.otherRuleList = (this.row.ruleList || []).filter(
          (it) => it.conditionType != 1
        )
        // 取出当前规则
        let ruleList = (this.row.ruleList || []).filter(
          (it) => it.conditionType == 1
        )
        if (ruleList && ruleList.length == 0) {
          ruleList = [{ state: 2, checked: 0 }]
        }
        this.ruleList = (ruleList || []).map((it) => {
          // 设置状态
          const arr = (it.ruleValue && JSON.parse(it.ruleValue)) || []
          if (((arr || []).find((its) => its.code == 1) || {}).checked == 1) {
            it.state = 3
          }
          if (((arr || []).find((its) => its.code == 2) || {}).checked == 1) {
            it.state = 1
          }
          if (((arr || []).find((its) => its.code == 3) || {}).checked == 1) {
            it.state = 2
          }
          if (((arr || []).find((its) => its.code == 4) || {}).checked == 1) {
            it.state = 4
          }
          return {
            ...it,
          }
        })
      } else {
        this.ruleList = this.ruleList.map((it) => {
          return {
            ...it,
            state: 2,
          }
        })
      }
    },
    handleCondion(item, index) {
      this.currentIndex = index
      this.optionsVisible = true
      this.itemData = item
    },
    // 选择单选框
    changeState(val, index) {},
    handleAdd(index) {
      this.ruleList.push({
        state: 2,
      })
    },
    handleDel(index) {
      this.ruleList.splice(index, 1)
    },
    handleClose() {
      this.$emit('input', false)
    },
    // 条件设置
    setCondition(data) {
      const info = this.ruleList[data.currentIndex]
      this.$set(this.ruleList, data.currentIndex, {
        ...info,
        ...data,
      })
    },
    confirm() {
      const ruleList = (this.ruleList || []).map((it, idx) => {
        const arr = [
          { code: 1, name: '隐藏', checked: it.state == 3 ? 1 : 0 },
          { code: 2, name: '仅显示', checked: it.state == 1 ? 1 : 0 },
          { code: 3, name: '选填', checked: it.state == 2 ? 1 : 0 },
          { code: 4, name: '必填', checked: it.state == 4 ? 1 : 0 },
        ]

        return {
          conditionType: 1,
          ruleText: it.ruleText,
          ruleTextShow: it.ruleTextShow,
          ruleValue: JSON.stringify(arr),
          relatedRuleFieldList: it.relatedRuleFieldList,
        }
      })
      // 拼接所有规则
      const data = this.otherRuleList.concat(ruleList)
      this.$emit('setFieldPermission', data)
      this.handleClose()
    },
  },
}
</script>
<style lang="scss" scoped>
.set-field-permission {
  &-content {
    width: 100%;
    &-item {
      display: flex;
      justify-content: space-between;
      margin-bottom: 10px;
      &-condition {
        width: 90%;
        background: #faf9f9;
        border-radius: 4px;
        padding: 20px;
        box-sizing: border-box;
        .list-row {
          font-weight: 400;
          font-size: 14px;
          color: #333333;
          .title-tips {
            color: #999999;
            margin-left: 10px;
          }
        }
      }
      &-opertaion {
        width: 10%;
        padding: 20px;
        box-sizing: border-box;
        display: flex;
        flex-direction: column;
        justify-content: space-between;
        img {
          width: 32px;
          height: 32px;
          cursor: pointer;
        }
      }
    }
  }
}
</style>
